﻿using Adnc.Application.Shared.Services;
using Adnc.Infra.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WYT.Ord.Application.Contracts.Dtos.Coupon;
using WYT.Ord.Application.Contracts.Services;
using WYT.Ord.Repository.Entities;

namespace WYT.Ord.Application.Services
{
    /// <summary>
    /// 优惠券服务
    /// </summary>
    public class CouponAppService : AbstractAppService, ICouponAppService
    {
        private readonly IEfRepository<Coupon> _couponRepository;

        private readonly IEfRepository<UserCoupon> _usrCouponRepository;

        //体验装优惠券
        private readonly long ExperienceCoupon = 1;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="couponRepository"></param>
        /// <param name="usrCouponRepository"></param>
        public CouponAppService(IEfRepository<Coupon> couponRepository
            , IEfRepository<UserCoupon> usrCouponRepository)
        { 
            _couponRepository = couponRepository;
            _usrCouponRepository = usrCouponRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public  async Task<AppSrvResult<List<CouponDto>>> GetCouponAsync(long userId)
        {
            var result = new List<CouponDto>();
            List<UserCoupon> usrCoupons = await _usrCouponRepository.Where(c => c.UserId == userId && c.State == 1 && c.StartTime < DateTime.Now && c.EndTime > DateTime.Now).ToListAsync();
            if (usrCoupons.Count == 0)
                return result;
            var couponIds = usrCoupons.Select(c => c.CouponId).Distinct().ToList();
            List<Coupon> coupons = _couponRepository.Where(x => couponIds.Contains(x.Id)).ToList();

            return (from c in coupons
             join u in usrCoupons on c.Id equals u.CouponId
             select new CouponDto
             {
                 Id = u.Id,
                 CouponId = c.Id,
                 Amount = c.Amount,
                 CouponName = c.Name,
                 CouponType = c.Type,
                 GoodsAstrict = c.GoodsAstrict,
                 AvailableGoods = c.AvailableGoods.ToLongArray(",", StringSplitOptions.RemoveEmptyEntries),
                 IsSuperposition = c.IsSuperposition,
                 MaxAmount = c.MaxAmount,
                 ReachAmount = c.ReachAmount,
                 Instructions = c.Instructions,
                 StartTime = u.StartTime,
                 EndTime = u.EndTime,
                 State = u.State,
             }).ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<AppSrvResult<UserDeductionCouponDto>> GetUserDeductionCouponInfor(long userId)
        {
            UserDeductionCouponDto result = new UserDeductionCouponDto();
            var data = await _usrCouponRepository.Where(x => x.UserId == userId && x.CouponId == ExperienceCoupon).ToListAsync();
            result.Unused = data.Count(x => x.State == 1 && x.StartTime > DateTime.Now);
            var current = data.FirstOrDefault(x => x.State == 1 && x.StartTime <= DateTime.Now && x.EndTime >= DateTime.Now);
            if (current != null)
            {
                result.Available = true;
                result.CouponID = current.Id;
            }
            else
                result.Available = false;
            return result;
        }
    }
}
